using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Utils;

public class StoryMap : MonoBehaviour
{
    public Transform pos0, pos1, pos2, pos3, pos4;

    public GameObject room0, room1, room2, room3, room4;

    public RoomController rm1c, rm3c, rm4c;

    public Monster monster1, monster2, monsterBoss;

    private void Awake()
    {
        rm1c = room1.GetComponent<RoomController>();
        rm3c = room3.GetComponent<RoomController>();
        rm4c = room4.GetComponent<RoomController>();
    }

    private void OnEnable()
    {
        InitialPosition();
    }

    void Update()
    {
        int num = GameController.Instance().archiveCtr.TempLevelData.storyRoomNum;
        if (monster1.monsterState == MonsterState.Die && num < 2)
        {
            GameController.Instance().archiveCtr.TempLevelData.storyRoomNum = 2;
            UITool.GetChildGameObject("SeparatorGroup", room0).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room1).SetActive(false);
            GameController.Instance().FSM.CurPlayerState = PlayerState.NotFight;
            if (monster1.roomCtr.enabled)
            {
                GameController.Instance().weaponCtr.Weapon.ResetForward();
            }
            // monster1.roomCtr.inFightState = false;
            // monster1.roomCtr.enabled = false;
            rm1c.inFightState = false;
            rm1c.enabled = false;
            monster1.gameObject.SetActive(false);
        }

        if (monster2.monsterState == MonsterState.Die && num < 4)
        {
            GameController.Instance().archiveCtr.TempLevelData.storyRoomNum = 4;
            UITool.GetChildGameObject("SeparatorGroup", room0).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room1).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room2).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room3).SetActive(false);
            GameController.Instance().FSM.CurPlayerState = PlayerState.NotFight;
            if (monster2.roomCtr.enabled)
            {
                GameController.Instance().weaponCtr.Weapon.ResetForward();
            }
            // monster1.roomCtr.inFightState = false;
            // monster2.roomCtr.inFightState = false;
            // monster1.roomCtr.enabled = false;
            // monster2.roomCtr.enabled = false;
            rm1c.inFightState = false;
            rm1c.enabled = false;
            rm3c.inFightState = false;
            rm3c.enabled = false;
            monster1.gameObject.SetActive(false);
            monster2.gameObject.SetActive(false);
        }

        if (monsterBoss.monsterState == MonsterState.Die && num < 5)
        {
            GameController.Instance().archiveCtr.TempLevelData.storyRoomNum = 5;
            UITool.GetChildGameObject("SeparatorGroup", room0).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room1).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room2).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room3).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room4).SetActive(false);
            GameController.Instance().FSM.CurPlayerState = PlayerState.NotFight;
            if (monsterBoss.roomCtr.enabled)
            {
                GameController.Instance().weaponCtr.Weapon.ResetForward();
                GameController.Instance().panelCtr.RemoveBossPanel();
            }
            // monster1.roomCtr.inFightState = false;
            // monster2.roomCtr.inFightState = false;
            // monsterBoss.roomCtr.inFightState = false;
            // monster1.roomCtr.enabled = false;
            // monster2.roomCtr.enabled = false;
            // monsterBoss.roomCtr.enabled = false;
            rm1c.inFightState = false;
            rm1c.enabled = false;
            rm3c.inFightState = false;
            rm3c.enabled = false;
            rm4c.inFightState = false;
            rm4c.enabled = false;
            monster1.gameObject.SetActive(false);
            monster2.gameObject.SetActive(false);
            // monsterBoss.gameObject.SetActive(false);
        }
    }

    private void InitialPosition()
    {
        int num = GameController.Instance().archiveCtr.TempLevelData.storyRoomNum;
        if (num == 0)
        {
            GameController.Instance().playerCtr.transform.position = pos0.position;
        }
        else if (num == 1)
        {
            GameController.Instance().playerCtr.transform.position = pos0.position;
            UITool.GetChildGameObject("SeparatorGroup", room0).SetActive(false);
        }
        else if (num == 2)
        {
            GameController.Instance().playerCtr.transform.position = pos1.position;
            UITool.GetChildGameObject("SeparatorGroup", room0).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room1).SetActive(false);
            monster1.gameObject.SetActive(false);
        }
        else if (num == 3)
        {
            GameController.Instance().playerCtr.transform.position = pos2.position;
            UITool.GetChildGameObject("SeparatorGroup", room0).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room1).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room2).SetActive(false);
            monster1.gameObject.SetActive(false);
        }
        else if (num == 4)
        {
            GameController.Instance().playerCtr.transform.position = pos3.position;
            UITool.GetChildGameObject("SeparatorGroup", room0).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room1).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room2).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room3).SetActive(false);
            monster1.gameObject.SetActive(false);
            monster2.gameObject.SetActive(false);
        }
        else if (num==5)
        {
            GameController.Instance().playerCtr.transform.position = pos4.position;
            UITool.GetChildGameObject("SeparatorGroup", room0).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room1).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room2).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room3).SetActive(false);
            UITool.GetChildGameObject("SeparatorGroup", room4).SetActive(false);
            monster1.gameObject.SetActive(false);
            monster2.gameObject.SetActive(false);
            monsterBoss.gameObject.SetActive(false);
        }
    }
    
    
}